/*
 *   Copyright (C) 2008 - 2009  FireBalls
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file FPS.h This file contains everything that has to do with sycronizing.
 */

// define
#ifndef __FPS__
#define __FPS__

// includes
#include <SDL/SDL.h>
#include "../io/Log.h"

/**
 * Syncronizer. A class that makes sure the game is running at a constant speed.
 */
class FPS
{
    public:

        /**
         * Singleton method.
         * @return an instance of this class
         */
        static FPS* singleton();

        /**
         * Destruktor. Deletes the needed memory for the log file object.
         */
        ~FPS()
        {
            if(logFPS != 0)
                delete logFPS;
        }

        /**
         * Init method.
         * @param timePerRound time a round should take
         * @param path path to the log file
         */
        void Init(unsigned char timePerRound, const char* path);
        void Init(unsigned char timePerRound);

        /**
         * Loop method. Used once per game loop. Delays the game so it runs at constant time.
         */
        void Loop();

    private:

        static FPS *instance;           ///< pointer to an object of this class

        /**
         * Private constructor. Makes sure nobody creates an object of this class in the regular way.
         */
        FPS() { }

        /**
         * Private copy constructor.
         * @see FPS()
         */
        FPS(const FPS&);

        /**
         * Guard class. Makes sure the instance is deleted.
         */
        class Guard {
            public:
                /**
                 * Destructor. Deletes the instance.
                 */
                ~Guard()
                {
                    if(FPS::instance != 0)
                        delete FPS::instance;
                }
        };
        friend class Guard;

        unsigned int next;              ///< Needed to calculate the delay. Normaly now + interval.
        unsigned char interval;         ///< Time a round should take. E.g. 30. 1000ms / 30ms = 33.3fps.
        unsigned int count;             ///< Counts the rounds (frames).
        unsigned int startCount;        ///< Safes the start time of a second. Needed to tell if a second has past by.
        unsigned int now;               ///< Safes the current time each round.

        bool logActive;                 ///< If the log file is in use.
        Log *logFPS;                    ///< Pointer to log file object.

};

#endif
